//
//  EJSHorizontalGoodsCCell.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2017/12/20.
//  Copyright © 2017年 lifayu. All rights reserved.
//

#import "EJSHorizontalGoodsCCell.h"
#import "EJSGoodsModel.h"
#import "EJSFlashGoodCollectionViewCell.h"
#import "EJSGoodsDetailInfoViewController.h"
#import "EJSAddCartButton.h"
#import "XOShopViewModel.h"
@interface EJSHorizontalGoodsCCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) NSMutableArray *goodsList;

@end

@implementation EJSHorizontalGoodsCCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"EJSFlashGoodCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"EJSGoodsCCell"];
}
- (void)setGoodsList:(NSMutableArray *)goodsList{
    _goodsList = [NSMutableArray arrayWithArray:goodsList];
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.goodsList.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    EJSFlashGoodCollectionViewCell *goodsCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EJSGoodsCCell" forIndexPath:indexPath];
    EJSGoodsModel *goodModel = [self.goodsList objectAtIndex:indexPath.row];
    [goodsCell.iconView setImageWithUrl:goodModel.cover];
    goodsCell.priceLabel.text = [NSString stringWithFormat:@"€ %@",goodModel.price];
    goodsCell.nameLabel.text = goodModel.spuName;
    return goodsCell;
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 15, 0, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  
//    CGFloat cellW = (ScreenWidth-60)/4.0;
//    return CGSizeMake(cellW, cellW + 46);
    return CGSizeMake(114, 166);
    
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    EJSGoodsDetailInfoViewController *goodsDetailVC = [[EJSGoodsDetailInfoViewController alloc] initWithNibName:@"EJSGoodsDetailInfoViewController" bundle:nil];
    EJSGoodsModel *goodModel = self.goodsList[indexPath.row];
    goodsDetailVC.goodsId = goodModel.spuId;

//    EJSGoodsSkuModel *skuModel = goodModel.skus.firstObject;
//    [goodsDetailVC setDatasWith:self.goodsList and:skuModel.shopId and:indexPath andCurrentGoodModel:goodModel];
//    goodsDetailVC.delegate = self;
    [[self getViewController].navigationController pushViewController:goodsDetailVC animated:YES];
}
#pragma mark 获取当前cell的viewController
- (UIViewController *)getViewController
{
    for (UIView *next = [self superview]; next; next = next.superview)
    {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]])
        {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}
- (void)goodsDetailWillDisappearWithIndex:(NSIndexPath *)indexPath {
    [[XOShopCartViewModel shared] updateCartList:^(XOShoppingCartListModel * _Nonnull response) {
        
    }];

}
@end
